<?php
function bt_subscriber_new($name,$email,$message= "<script>alert('Thank you, subcribed successfully!')</script>"){	
				$password = wp_generate_password(6, false);
				$userdata	=	array(
					'user_login'		=>$email,
					'user_password'		=>$password,
					'user_email'		=>$email,
					'role'				=>'subscriber',
					'display_name'		=>$name		
			     	);
				$user_id	=	wp_insert_user($userdata);
				if(isset($user_id) && $user_id>0){
					
					$code	=	wp_generate_password(30, false);
					add_user_meta($user_id, 'wp_subscrib_active_code',$code);
					update_user_meta($user_id, 'newsletter', 'subcribe');
					
					send_subcribe_thank_you($name, $email,$code,$user_id);
					
					echo $message;
				}
					

	}
	
function send_subcribe_thank_you($subcriber, $emailto,$code,$user_id){
				include	'xtemplate.class.php';
				$header   	= 'Content-type: text/html; charset=utf-8\r\n';
				
				$title = "You have subcribed at " . get_bloginfo('name');
				$contact_email = get_option('contactemail');
				if(!is_email($contact_email)) $contact_email = get_option('admin_email');
				$contact_name = get_option('contact_name');
				if(!$contact_name)$contact_name = get_bloginfo('name');
				
				$parseTemplate	=	new XTemplate('xtemplate-email-subscrib-thank-you.html');
				$parseTemplate->assign('subscriber',$subcriber);
				$parseTemplate->assign('link', get_bloginfo('home'));
				$parseTemplate->assign('website', get_bloginfo('name'));
				$parseTemplate->assign('title',$title);
				$parseTemplate->assign('code',$code);
				$parseTemplate->assign('user_id',$user_id);	
				$parseTemplate->assign('contact_email',$contact_email);	
				$parseTemplate->assign('contact_name',$contact_name);					
				$parseTemplate->parse('main');
				
				
				wp_mail($emailto, $title, $parseTemplate->text('main'), $header);
}

function bt_check_before_unsubscrib(){
	if(!empty($_GET['user_id'])){
		if(!empty($_GET['unsubcribe'])){
			bt_unsubscriber($_GET['user_id'], $_GET['unsubcribe']);
		}
	}
}


function bt_unsubscriber($user_id, $code){
	
	global $wpdb;
	$user_code	=	get_user_meta($user_id, 'wp_subscrib_active_code', true);
	if($user_code==$code){
		
		 update_user_meta($user_id, 'newsletter', 'unsubcribe');
		
		
		echo "<script>alert('Unsubcribed successfully!')</script>";

	} 
}

function bt_send_subscrib(){
	
	include	'xtemplate.class.php';
	global $post;
	$post_id	=	$post->ID;
	if( !get_post_meta($post_id, 'newsletter',true) && ( $post->post_type =='post')){	
		
		
		$title 		=   $post->post_title;
		$desc 		= 	get_the_excerpt();
		$subject	= 'News from ' . get_bloginfo('name');
		$header   	= 'Content-type: text/html; charset=utf-8\r\n';
		
		$user_args = array(
					'meta_key' => 'newsletter',
					'meta_value' => 'subcribe',
					'meta_compare' => '=',
				 );
				 
		$users	=	get_users($user_args);
		if($users){
			foreach ($users as $user){
			
				 
				$code = get_user_meta($user->ID, 'wp_subscrib_active_code',true);
				
				$parseTemplate	=	new XTemplate('xtemplate-email-subscrib.html');
				$parseTemplate->assign('subscriber',$user->display_name);
				$parseTemplate->assign('link', get_permalink($post_id));
				$parseTemplate->assign('website', get_bloginfo('home'));
				
				$parseTemplate->assign('homepage',get_bloginfo('url'));
				
				$parseTemplate->assign('user_id',$user->ID);
				$parseTemplate->assign('title',$title);
				$parseTemplate->assign('desc',$desc);
				$parseTemplate->assign('code',$code);
				
				$parseTemplate->parse('main.messager');
				$parseTemplate->parse('main');
				
				wp_mail($user->user_email, $subject, $parseTemplate->text('main'), $header);
				update_post_meta($post_id, 'newsletter', 'send',true);
			}	
		}
	}
}



add_action('publish_post','bt_send_subscrib');
add_action('init','bt_check_before_unsubscrib');
